﻿#pragma once

#include <QGraphicsObject>
class GraphicsBaseItem;

class GraphicsPinkItem : public QGraphicsObject
{
	Q_OBJECT

public:
	enum PinkItemType {
		// 4个角点
		TopLeft_Enum,
		TopRight_Enum,
		BottomRight_Enum,
		BottomLeft_Enum,
		CornerEnd_Enum,// 角点分割点
		// 4个中点
		MidTop_Enum,
		MidRight_Enum,
		MidBottom_Enum,
		MidLeft_Enum,
		MidEnd_Enum,// 中点分割点
		// 1个旋转点
		Rotation_Enum,
		RotationEnd_Enum,
		// 连接线
		LeftLine_Enum,
		TopLine_Enum,
		RightLine_Enum,
		BottomLine_Enum
	};
	Q_ENUM(PinkItemType);

	GraphicsPinkItem(PinkItemType type, GraphicsBaseItem* parent = nullptr);
	~GraphicsPinkItem();
	PinkItemType getPinkItemType()const { return m_type; }
	bool isControling()const { return m_isControling; }
	void setItemScale(qreal scale);
	// 带有缩放系数
	QSize getScaleSize()const { return m_size * m_scale; }
	// 是否显示旋转角度
	void setAngleVisable(bool isAngleVisable);
	// 缩放图标
	QCursor getCornerCursor();
public slots:
	void onUpdatePos();
	void onUpdateCursor();
private:
	// 更新父图元的尺寸
	void updateParentGraphcisGeometry(const QPointF& delta);
protected:
	virtual QRectF boundingRect() const override;
	virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr)override;
	virtual void mousePressEvent(QGraphicsSceneMouseEvent* event)override;
	virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event)override;
	virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event)override;
	virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* event)override;
	virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* event)override;
private:
	QSize m_size = QSize(18, 18);
	const PinkItemType m_type;
	QPointF m_lastScenePos;
	QPointF m_pressScenePos;
	const int m_penWidth = 2;
	bool m_isHover = false;
	GraphicsBaseItem* m_parentItem = nullptr;
	// 是否正在控制中
	bool m_isControling = false;
	QImage m_roationImage;
	QImage m_cornerImage;
	// 缩放系数
	qreal m_scale = 1.25;
	// 是否显示旋转角度
	bool m_isAngleVisable = false;
};

using GraphicsPinkItemType = GraphicsPinkItem::PinkItemType;